﻿using project.B12.Model.BaseModel;
using software.Common.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace project.B12.Model.Models
{
    /// <summary>
    /// 药敏板数据汇总.
    /// </summary>
    public class WellPlate : ObservableObject
    {
        public WellPlate(int count)
        {
            for (int i = 0; i < count; i++)
            {
                ImageList.Add(new Test() 
                {
                    Index = i,
                });
            }
        }
        
        /// <summary>
        /// 药敏板ID
        /// </summary>
        private string id = string.Empty;
		public string ID
        {
			get { return id; }
			set { id = value; this.RaisePropertyChanged(); }
		}

        /// 药敏板类型
        /// </summary>
        private string plateType = string.Empty;
        public string PlateType
        {
            get { return plateType; }
            set { plateType = value; this.RaisePropertyChanged(); }
        }


        /// 药敏板菌信息
        /// </summary>
        private string bacteria = string.Empty;
        public string Bacteria
        {
            get { return bacteria; }
            set { bacteria = value; this.RaisePropertyChanged(); }
        }


        /// <summary>
        /// 临时记录点击位置，避免UI卡顿
        /// </summary>
        private int selectLocation = -1;
        public int SelectLocation
        {
            get { return selectLocation; }
            set { selectLocation = value; this.RaisePropertyChanged(); }
        }


        /// <summary>
        /// 模板图片数据源清单
        /// </summary>
        private ObservableCollection<Test> imageList = new ObservableCollection<Test>();
		public ObservableCollection<Test> ImageList
        {
			get { return imageList; }
			set { imageList = value; this.RaisePropertyChanged(); }
		}

        private ObservableCollection<Test> report=new ObservableCollection<Test>();
        public ObservableCollection<Test> Report
        {
            get { return report; }
            set
            {
                report = value;
                this.RaisePropertyChanged();
            }
        }

        public void AddReport(string drugName,string sirResult,Table_SIR sir,string mic)
        {
            Report.Add(new Test()
            {
                DrugName = drugName,
                S = sir.S,
                I = sir.I,
                R = sir.R,
                MIC = mic,
                SIRResult = sirResult
            });
        }

        /// <summary>
        /// 将扫描图像数据\判读结果数据更新到微孔板数据.
        /// </summary>
        /// <param name="_id"></param>
        /// <param name="_bitmapImages"></param>
        /// <param name="_results"></param>
        /// <returns></returns>
        public MessageModel UpdateData(string _id, BitmapImage[]? _bitmapImages, ResultMes[] _results, MicroplateMes? _Mm,string plateType,string bacterialName)
        {
            if (string.IsNullOrEmpty(_id))
            {
                return MessageModel.ReturnFail("The WellPlate ID cannot be empty!");
            }
            if (_bitmapImages == null)
            {
                return MessageModel.ReturnFail("The bitmapImages cannot be empty!");
            }
            if (_bitmapImages!=null && _bitmapImages.Count() != ImageList.Count)
            {
                return MessageModel.ReturnFail("The BitmapImages and templates are different!");
            }
            PlateType = plateType;
            Bacteria = bacterialName;
            ObservableCollection<Test> temp = new();
            for (int i = 0; i < ImageList.Count; i++)
            {
                temp.Add(new Test() { Index = i });
            }
            ID = _id;
            SelectLocation = 0;
            for (int i = 0; i < temp.Count; i++)
            {
                temp[i].ImageSource = _bitmapImages[i];
            }
            int count = temp.Count > _results.Length ? _results.Length : temp.Count;
            for (int i = 0; i < count; i++)
            {
                
                if (_Mm == null)
                {

                }
                else
                {
                    temp[i].DrugName = _Mm.antibiotic_concentration[i].Antibiotic.Contains(",") ?
                    _Mm.antibiotic_concentration[i].Antibiotic.Split(',')[0] :
                    _Mm.antibiotic_concentration[i].Antibiotic;
                    temp[i].DrugConcentration = _Mm.antibiotic_concentration[i].Antibiotic.Contains(",") ?
                    float.Parse(_Mm.antibiotic_concentration[i].Antibiotic.Split(',')[1]) : null;
                }
                if (_results[i] == null)
                {
                    continue;
                }
                temp[i].Result = _results[i].ret;
                temp[i].Ave = _results[i].ave;
                temp[i].Max = _results[i].max;
                temp[i].Min = _results[i].min;
                temp[i].Cnt = _results[i].cnt;
                temp[i].CV = _results[i].cv;
                temp[i].AI = _results[i].ai;
            }

            ImageList = temp;
            return MessageModel.ReturnSuccess("Import was successful!");
        }
    
    }
}
